package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.CustomerLocation;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.CustomerLocationDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2021-06-08 14:12:18
 */
public class CustomerLocationConverts {

    private static Logger LOG = LoggerFactory.getLogger(CustomerLocationConverts.class);

    public static List<CustomerLocationDTO> convert(List<CustomerLocation> customerLocationList) {
        if (CollectionUtils.isEmpty(customerLocationList)) {
            return Collections.EMPTY_LIST;
        }

        List<CustomerLocationDTO> customerLocationListResult = Lists.newArrayList();
        for (CustomerLocation customerLocation : customerLocationList) {
            customerLocationListResult.add(convert(customerLocation));
        }
        return customerLocationListResult;
    }

    public static CustomerLocationDTO convert(CustomerLocation customerLocation) {
        try {
            return BeanUtils.copy(customerLocation, CustomerLocationDTO.class);
        } catch (Exception e) {
            LOG.error("CustomerLocation到CustomerLocationDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "CustomerLocation到CustomerLocationDTO转换失败", e);
        }
    }

    public static CustomerLocation convert(CustomerLocationDTO customerLocationDTO) {
        try {
            return BeanUtils.copy(customerLocationDTO, CustomerLocation.class);
        } catch (Exception e) {
            LOG.error("CustomerLocationDTO到CustomerLocation转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "CustomerLocationDTO到CustomerLocation转换失败", e);
        }
    }
}